import { ref } from 'vue'
import { addSchedule } from '@/api/schedule.js'
import { getUser } from '@/utils/auth'
import { getCurrentTimeFormatted, calculateTimeDifference } from '@/utils/date.js'
export default function () {
	//设置定时器记录页面，浏览时间
	//取出当前登陆人的userid，如果被关注用户的id等于当前登陆人的id，则不显示关注字眼
	const userid = getUser()
	const postId = uni.getStorageSync('帖子id')
	console.log(postId,'帖子id帖子id帖子id')
	const viewTimeOutId = ref()
	const viewedAt = ref()
	const startViewTimer = (() => {
		// viewTimeOutId.value = setTimeout(() => {
		// 	//当页面浏览到一定时间，将页面记录下来

		// 	console.log('学习记录页面已被浏览超过10秒');
		// }, 10000);
		viewTimeOutId.value = getCurrentTimeFormatted();

		console.log(viewTimeOutId.value, 'viewTimeOutId.value')
	})
	const clearViewTimer = (() => {
		// if (viewTimeOutId.value !== null) {
		clearTimeout(viewTimeOutId.value);
		viewedAt.value = getCurrentTimeFormatted();
		uni.removeStorageSync('帖子id')
		// if (calculateTimeDifference(viewedAt.value, viewTimeOutId.value) > 10)
		// console.log(viewedAt.value, 'viewedAt.value666')
		// viewTimeOutId.value = null;
		// }

		addSchedule({
			postId: postId,
			userId: userid,
			browseTime: calculateTimeDifference(viewedAt.value, viewTimeOutId.value)
		}).then((res) => {
			console.log(res,'65555564')
			console.log('定时器已清除,浏览结束时间为：', calculateTimeDifference(viewedAt.value, viewTimeOutId.value))
		})

	})
	return { viewTimeOutId, viewedAt, startViewTimer, clearViewTimer }
}